import java.io.*;
import java.sql.*;

import org.w3c.dom.*;
import javax.xml.parsers.*;
import java.io.File;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Scanner;


public class XML_Receiver {

	// JDBC driver name and database URL
	static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
	static final String DB_URL = "jdbc:mysql://127.0.0.255:3306/iLink";
	//  Database credantials
	static final String USER = "root";
	static final String PASS = "";

	public String filename="";
	public String ip_add="";
	
	public XML_Receiver(String nam,String ip){
		filename=nam;
		ip_add=ip;
		receive();
	}

	public void receive(){
		Connection con = null;

		try {
			//STEP 2: Register JDBC driver
			Class.forName("com.mysql.jdbc.Driver");

			//STEP 3: Open a connection
			System.out.println("Connecting to database...");
			con = DriverManager.getConnection(DB_URL,USER,PASS);

			PreparedStatement st=null;

			DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
			Document doc = docBuilder.parse (new File(filename));
			doc.getDocumentElement ().normalize ();
			System.out.println ("Root element of the doc is " + doc.getDocumentElement().getNodeName());


			NodeList ListofFiles = doc.getElementsByTagName("Details");
			for(int s=0; s<ListofFiles.getLength() ; s++){
				Node fileNode = ListofFiles.item(s);
				if(fileNode.getNodeType() == Node.ELEMENT_NODE){
					Element fileElement = (Element)fileNode;
					NodeList NameList = fileElement.getElementsByTagName("Name");
					Element NameElement = (Element)NameList.item(0);
					NodeList textnameList = NameElement.getChildNodes();
					String name= ((Node)textnameList.item(0)).getNodeValue().trim();
					NodeList dateList = fileElement.getElementsByTagName("location");
					Element dateElement = (Element)dateList.item(0);
					NodeList textdateList = dateElement.getChildNodes();
					String location= ((Node)textdateList.item(0)).getNodeValue().trim();
					NodeList sizeList = fileElement.getElementsByTagName("size");
					Element sizeElement = (Element)sizeList.item(0);
					NodeList textsizeList = sizeElement.getChildNodes();
					long size= Long.parseLong(((Node)textsizeList.item(0)).getNodeValue().trim());
					NodeList lastmodified = fileElement.getElementsByTagName("date");
					Element lastmodifiedElement = (Element)lastmodified.item(0);
					NodeList textmodifiedList = lastmodifiedElement.getChildNodes();
					long date= Long.parseLong(((Node)textmodifiedList.item(0)).getNodeValue().trim());
				
					long datetime=date;
					Date d = new Date(datetime);
	                SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
	                String dateString = sdf.format(d);
	                
					
					
					NodeList sourceList = fileElement.getElementsByTagName("source");
					Element sourceElement = (Element)sourceList.item(0);
					NodeList textsourceList = sourceElement.getChildNodes();
					String source= ((Node)textsourceList.item(0)).getNodeValue().trim();
					st=con.prepareStatement("insert into files values(?,?,?,?,?)");
					st.setString(1, name);
					st.setString(2, location);
					st.setLong(3, size);
					st.setString(4, dateString);
					st.setString(5, ip_add);	
					st.executeUpdate();
				}
			}
		}catch (Exception err) {
			//err.printStackTrace();
			System.out.println(" " + err.getMessage ());
		}

	}

}